﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using WebApplication2.Model;
using WebApplication2.Server;
using WebApplication2.Util;





namespace WebApplication2.Handler
{
    /// <summary>
    /// BudgetHandler 的摘要说明
    /// </summary>
    public class BudgetHandler : IHttpHandler ,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string funC = context.Request["func"];
            try
            {
                switch (funC)
                {
                    case "add":
                        add(context);
                        break;
                    case "query":
                        query(context);
                        break;
                    case "updateless":
                        updateless(context);
                        break;
                    case "delete":
                        delete(context);
                        break;
                    default:
                        context.Response.Write("{data:\"参数错误！\"}");
                        Console.WriteLine("傻逼,参数错误！");
                        break;
                }
            }
            catch (Exception e)
            {
                context.Response.ClearContent();
                context.Response.Write(e.ToString());
            }
        }
        /// <summary>
        /// 返回1：成功
        /// 返回2：pr_id为空，找不到pr编号为传入pr_id的pr单
        /// 返回0：http code 500 服务器错误
        /// </summary>
        /// <param name="context"></param>
        private void updateless(HttpContext context)
        {
            string msg = "";
            string json = "";
            int res = 0;
            string pr_id = context.Request["pr_id"];
            string sql1 = "select * from T_Budget where PR_ID= '" + pr_id + "'";
            DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql1).Tables[0].Rows;
            //查询同名PR单号是否为空
            if (rows.Count != 0)
            {
                //string sql = "insert into T_Budget (ProJect_ID,PR_Budget,PR_ID,B_Applicants,B_AppTime,B_Content,C_ShopMan,C_ID,C_Time,C_Budget) values('" + context.Request["project_id"] + "'," + Convert.ToInt32(context.Request["pr_budget"]) + ",'" + context.Request["pr_id"] + "','" + context.Request["applicants"] + "','" + context.Request["apptime"] + "','" + context.Request["context"] + "','" + context.Request["shopman"] + "','" + context.Request["c_id"] + "','" + context.Request["c_time"] + "'," + context.Request["c_budget"] + ")";
                string sql = "update T_Budget set C_Budget="+context.Request["c_budget"] +",C_ID='"+context.Request["c_id"] +"',C_Time='"+context.Request["c_time"] +"',C_ShopMan='"+context.Request["shopman"] +"' where PR_ID='"+context.Request["pr_id"] +"'and ProJect_ID='"+context.Request["id"]+"'";
                try
                {
                    res = MrDBAccess.ExecuteNonQuery(sql);
                }
                catch (Exception e)
                {
                    msg = e.ToString();

                }
                if (res == 1)
                {
                    json = "{\"Code\":\"" + res + "\",\"Msg\":\"" + msg + "\"}";
                }
                else
                    json = "{\"Code\":\"" + 0 + "\",\"Msg\":\"" + msg + "\"}";
            }
            else json = "{\"Code\":\"" + 2 + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);
        }

        private void delete(HttpContext context)
        {
            int res = 0;
            string msg = "问问我是否有在听你说话吧，剧透一下，我没在听哦";
            string json = "";
            string project_id = context.Request["project_id"];
            string pr_id = context.Request["pr_id"];
            string sql = "delete from T_Budget where PR_ID='"+pr_id+"'";
             res = MrDBAccess.ExecuteNonQuery(sql);
            json = "{\"Code\":\"" + res + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);
        }
       

        private void query(HttpContext context)
        {
            string json = "";
            string project_id = context.Request["project_id"];
            string pr_id = context.Request["pr_id"];
            string sql = "select * from T_Budget where ProJect_ID= '" + project_id + "'";
            DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql).Tables[0].Rows;
            List<Budget> BudgetList = new List<Budget>();
            if (rows != null && rows.Count > 0)
            {
                BudgetList = getBudgetInfo(rows);
            }

            FeedBackMsg<List<Budget>> feedBack = new FeedBackMsg<List<Budget>>();
            feedBack.Code = 1;
            feedBack.Msg = "哈哈，没有去死吧——金克斯";
            feedBack.Obj = BudgetList;
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }




        private void add(HttpContext context)
        {
            string msg = "";
            string json = "";
            int res = 0;
            string pr_id = context.Request["pr_id"];
            string sql1 = "select * from T_Budget where PR_ID= '" + pr_id + "'";
            DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql1).Tables[0].Rows;
            //查询同名PR单号是否为空
            if (rows.Count ==0 )
            {
                string sql = "insert into T_Budget (ProJect_ID,PR_Budget,PR_ID,B_Applicants,B_AppTime,B_Content,C_ShopMan,C_ID,C_Time,C_Budget) values('" + context.Request["project_id"] + "'," + Convert.ToInt32(context.Request["pr_budget"]) + ",'" + context.Request["pr_id"] + "','" + context.Request["applicants"] + "','" + context.Request["apptime"] + "','" + context.Request["context"] + "','" + context.Request["shopman"] + "','" + context.Request["c_id"] + "','" + context.Request["c_time"] + "'," + context.Request["c_budget"] + ")";
                try
                {
                    res = MrDBAccess.ExecuteNonQuery(sql);
                }
                catch (Exception e)
                {
                    msg = e.ToString();
                 
                }
                if (res == 1)
                {
                    json = "{\"Code\":\"" + res + "\",\"Msg\":\"" + msg + "\"}";
                }
                else
                    json = "{\"Code\":\"" + 0 + "\",\"Msg\":\"" + msg + "\"}";
            }
            else json = "{\"Code\":\"" + 2 + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);
        }

        private List<Budget> getBudgetInfo(DataRowCollection rows)
        {
            if (rows != null && rows.Count > 0)
            {
                List<Budget> list = new List<Budget>();
                int iRowLength = rows.Count;
                for (int i = 0; i < iRowLength; i++)
                {
                    Budget budgetInfo = new Budget();

                    budgetInfo.C_id = DataBase.ObjectToString(rows[i]["C_ID"]);
                    budgetInfo.Pr_id = DataBase.ObjectToString(rows[i]["PR_ID"]);
                    budgetInfo.Project_id = DataBase.ObjectToString(rows[i]["ProJect_ID"]);
                    budgetInfo.Pr_budget = DataBase.ObjectToInt(rows[i]["pr_budget"]);
                    budgetInfo.C_budget = DataBase.ObjectToInt(rows[i]["c_budget"]);
                    budgetInfo.Applicants = DataBase.ObjectToString(rows[i]["B_Applicants"]);
                    budgetInfo.Content = DataBase.ObjectToString(rows[i]["B_Content"]);
                    budgetInfo.Shopman = DataBase.ObjectToString(rows[i]["C_ShopMan"]);
                    budgetInfo.App_time = DateTime.Parse(DataBase.ObjectToString(rows[i]["B_AppTime"]));
                    budgetInfo.App_time = DateTime.Parse(DataBase.ObjectToString(rows[i]["C_Time"]));
                    list.Add(budgetInfo);
                }
                return list;
            }
            return null;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}